/*
Naijanimi Free SMS Application for Nokia mobiles.

"Naijanimi Free SMS Application" is protected by the copyright law
(c) 2011 Enrico Miglino / Tech Consulting (enrico.miglino@ovi.com)
Distributed under the LGPL 3.0 license.

This file is part of "Naijanimi Free SMS Application".

    "Naijanimi Free SMS Application" is free software: you can redistribute it
    and/or modify it under the terms of the Lesser GNU General Public License
    as published by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Naijanimi Free SMS Application" is distributed in the hope that it
    will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    Lesser GNU General Public License for more details.

    You should have received a copy of the Lesser GNU General Public License
    along with "Naijanimi Free SMS Application".
    If not, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 1.0
import "elements" 1.0 as Naijanimi  // Components folder

Item {
    id: screen  // This is the main screen block

    // Layers z-levels
    property int z_background: 0
    property int z_welcome: 2
    property int z_splash: 1
    property int z_formsblock: 3
    property int z_quitbtn: 4
    property int z_tabbedForm: 5

    // Application background
    Rectangle {
        id: background

        width: screen.width
        height: screen.height
        color: "#ffffff"    // white background
        z: screen.z_background
    }

    // Image background
    // Image backgroud is shown only when form manager is active and startup animation finished
    Image {
        id: backimg

        fillMode: Image.PreserveAspectFit
        z: screen.z_welcome
        scale: screen.width / backimg.width
        source: "elements/images/splash_man.png"
        opacity: 0
        anchors.horizontalCenter: screen.horizontalCenter
        anchors.verticalCenter: screen.verticalCenter
    }

    // ---------------------------------------------------------- START DEBUG BLOCK
    // Debug info.
    // For debug console messages use debugMessage(msg) instead of console.log(msg)
    // in the source module set baseMsg property with the string that should appear
    // before any debug message on the console.
    // To disable debug, set debugStatus to false. This can be done also only in some
    // code blocks.
    property bool debugStatus: true
    property string baseMsg: "*** Debug main.qml: "
    function debugMessage(msg) { if (debugStatus == true) console.log(baseMsg + msg); }
    // ---------------------------------------------------------- END DEBUG BLOCK

    property int waitDuration: 3000     // Default delay to show splash screen
    property int welcomeDuration: 4000  // Default delay to show welcome screen

    // Set the correct visibility for the all-in-one tabbed page with
    // program functions, accessibile after the user authenticated
    function showProgramFunctions() {
        allInOne.opacity = 1;
    }

    // Timer used to wait showing the splash screen.
    Timer {
        id: waitCycle
        interval: waitDuration; running: true; repeat: false
        onTriggered: {
            splashimage.state = "reanchored"
            welcomeimage.splashShown = true
            // Start the timer to show the welcome screen
            waitWelcome.running = true
        }
    }

    // Timer used to wait showing the welcome screen.
    Timer {
        id: waitWelcome
        interval: welcomeDuration; running: false; repeat: false
        onTriggered: {
            welcomeimage.splashHide = true
            programExit.opacity = 1
            formManager.opacity = 1
            backimg.opacity = 0.3
        }
    }

    /*********************************************************************
        Main screen layout

        Main screen shows login and register button and let the user
        to decide what to do.
    *********************************************************************/

    Naijanimi.FlipForms {
        id: formManager

        width: screen.width
        height: screen.height
        // Set parameters for vertical rotation
        angle: 180; yAxis: 1
        // Hide until intro animation ends.
        opacity: 0
        // Stay over the welcome image
        z: screen.z_formsblock

        onProgramfunctionsClicked: { screen.showProgramFunctions(); debugMessage("Program functions clicked") }
    }


    /*********************************************************************
        Splash screen, shown at the program startup and welcome page
    *********************************************************************/

    Naijanimi.Welcome {
        id: welcomeimage

        property bool splashShown: false // Set to true to appear automatically. If set to false, appear only
        // when the bool status changes (see behaviors below)
        property bool splashHide: true

        opacity: 0  // Initially hidden. Shows when the flag disappear
        z: screen.z_welcome
        scale: screen.width / welcomeimage.itemwidth

        Behavior on splashShown {
            NumberAnimation { target: welcomeimage; property: "opacity"; to: 1; duration: 2000 }
        }
        Behavior on splashHide {
            NumberAnimation { target: welcomeimage; property: "opacity"; to: 0; duration: 2000 }
        }
    }

    Naijanimi.Splash {
        id: splashimage // Splash image shown when program is in power-off state

        // scaleFactor is used to reposition and resize the objects on the screen
        // depending by the screen resolution.
        property real scaleFactor: screen.width / splashimage.adaptivewidth

        opacity: 1  // If splash screen is 0 by default it is not shown at startup
        z: screen.z_splash
        // Resize the object based on the screen size applying a scale factor
        scale: splashimage.scaleFactor

        // Defines the anchors.top so the state "reanchored" can modify the position
        // on exit
        anchors.top: screen.top
        anchors.topMargin: 5

        // Out state definition, managed by the animation transition
        states: State {
            name: "reanchored"
            AnchorChanges { target: splashimage; anchors.top: screen.bottom }
        }

        // Definition of the animation for the image on exit. The transition is connected
        // to the "reanchored" state, triggered by the wait timer of the splash screen on
        // program startup.
        transitions: Transition {
            AnchorAnimation {
                duration: 2000
                easing.amplitude: 1; easing.type: Easing.OutBounce
            }
        }
    }

    /*********************************************************************
        Close button always on top.
        Quit button is shown after the splash animation.
    *********************************************************************/

    Naijanimi.Quit {
        id: programExit

        opacity: 0
        z: screen.z_quitbtn
        anchors.right: screen.right
        anchors.top: screen.top
    }

    /************************************************************************
      Program functions
    ************************************************************************/

    // The program functions are all contained in LoggedFunctions form.
    // This is set invisible by default and its visibility changes only when
    // the user login correctly in he program.
    Naijanimi.LoggedFunctions {
        id: allInOne

        width: screen.width
        height: screen.height
        z: z_tabbedForm
        opacity: 0
    }
}

